package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.ResultBean;
import com.by.bean.Supplier;
import com.by.group.V;
import com.by.mapper.PaymentMapper;
import com.by.mapper.SupplierContactMapper;
import com.by.mapper.SupplierMapper;
import com.by.query.SupplierSave;
import com.by.query.SupplierStatus;
import com.by.query.SupplierUpdate;
import com.by.service.SupplierService;
import com.by.service.UserService;
import com.by.utils.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author :LYJ
 * @version : 1.0
 * @date : 2025/2/17 20:36
 * @desc :
 * @since : JDK17
 */
@RestController
@RequestMapping("/api")
@Api(tags = "供应商管理")
public class SupplierController extends BaseController {
    @Autowired
    SupplierService supplierService;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    SupplierContactMapper supplierContactMapper;
    @Autowired
    PaymentMapper paymentMapper;
    @Autowired
    UserService userService;

    //测试saToken鉴权
    //@SaCheckRole("super-admin")
    @PostMapping("/supplier/save")
    @ApiOperation("供应商保存")
    public ResultBean save(@Validated @RequestBody SupplierSave supplierSave, BindingResult result) {
        V.validate(result);
        supplierService.saveSupplier(supplierSave);
        return success("success", "保存成功");
    }

    @DeleteMapping("/supplier")
    @ApiOperation("供应商删除")
    public ResultBean delete(@Validated @RequestBody Supplier supplier, BindingResult result) {
        V.validate(result);
        int res = supplierService.delete(supplier.getId());
        Assert.isZero(res, "删除失败");
        return success("success", "删除成功");
    }

    @PutMapping("/supplier/update")
    @ApiImplicitParam(name = "supplier", value = "id", required = true)
    @ApiOperation("供应商修改")
    public ResultBean update(@Validated @RequestBody SupplierUpdate supplierUpdate, BindingResult result) {
        if (result.hasErrors()) {
            return fail("error", "输入数据验证失败");
        }

        try {
            supplierService.updateSupplier(supplierUpdate);
            return success("success", "修改成功");
        } catch (Exception e) {
            // 处理异常，比如日志记录等
            return fail("error", "修改失败，请检查输入数据或联系管理员");
        }
    }

    /**
     * 根据销售人员名称查询
     *
     * @param supplier
     * @param result
     * @return
     */
    @GetMapping("/supplier")
    @ApiOperation("供应商查询")
    public ResultBean queryByName(@Validated Supplier supplier, BindingResult result) {
        V.validate(result);
        return success("success", supplierMapper.queryByMap(BeanUtil.beanToMap(supplier)));
    }

    /**
     * 查询供应商列表
     *
     * @param supplier
     * @param result
     * @return
     */
    @PostMapping("/supplier/list")
    @ApiOperation("供应商列表")
    public ResultBean queryList(@Validated Supplier supplier, BindingResult result) {
        V.validate(result);
        return success("success", supplierMapper.queryByMap2(BeanUtil.beanToMap(supplier)));
    }

    @PutMapping("/supplier/changeStatus")
    @ApiOperation("修改供应商账号状态")
    public ResultBean updateStatus(@RequestBody SupplierStatus supplierStatus) {
        return success("success", supplierMapper.updateStatus(BeanUtil.beanToMap(supplierStatus)));
    }
}
